package com.azxcrm.project.system.user.controller;

import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.azxcrm.common.utils.StringUtils;
import com.azxcrm.framework.aspectj.lang.annotation.Log;
import com.azxcrm.framework.web.domain.Message;
import com.azxcrm.project.monitor.workflow.common.CommonService;
import com.azxcrm.project.monitor.workflow.domain.WorkFlowApply;
import com.azxcrm.project.monitor.workflow.service.WorkFlowApplyService;
import com.azxcrm.project.system.basic.domain.RegisterUser;
import com.azxcrm.project.system.basic.service.IRegisterUserService;
import com.azxcrm.project.system.dept.domain.Dept;
import com.azxcrm.project.system.dept.service.IDeptService;
import com.azxcrm.project.system.user.dao.IUserRoleDao;
import com.azxcrm.project.system.user.domain.User;
import com.azxcrm.project.system.user.service.IUserService;

/**
 * 注册
 * 
 * @author Wyl
 */
@Controller
@RequestMapping("/register")
public class RegisterContrller {

	@Autowired
	IUserRoleDao iUserRoleDao;
	
	@Autowired
	private IDeptService deptService;
	
	@Autowired
	private IUserService userService;
	
	@Autowired
	private CommonService commonService;
	
	@Autowired
	private IRegisterUserService registerUserService;
	
	@Autowired
	private WorkFlowApplyService workFlowApplyService;
	
	/**
	 * 部门经理角色Key
	 */
	@Value("${crm.role.departmentManager}")
	private String departmentManager;
	
    @GetMapping("/registerWap")
    public String registerWap(Model model)
    {
    	return "registerWap";
    }

	@Log(title = "注册", action = "获取部门信息")
	@GetMapping("/getDept")
	@ResponseBody
	public List<Dept> getDept() {
		List<Dept> deptList = deptService.selectDeptByParentId(215);
		return deptList;
	}
	
    @Log(title = "注册", action = "保存注册信息")
	@PostMapping("/saveRegisterUser")
	@ResponseBody
	public Message saveRegisterUser(RegisterUser registerUser, Model model) {
		RegisterUser reg = new RegisterUser();
		reg.setRegisterAuditType(0);
		reg.setRegisterPhonenum(registerUser.getRegisterPhonenum());
		reg.setRegisterDeptid(registerUser.getRegisterDeptid());
		if (StringUtils.isNotEmpty(registerUserService.selectRegisterUserList(reg))) {
			// 检测该业务员是否还有待处理的注册请求(手机号码+dept+register_audit_type)，有：不能提交
			return Message.error("还未审批通过无法重复提交");
		} else if (StringUtils.isEmpty(registerUser.getRegisterPassword())) {
			// 密码非空
			return Message.error();
		} else if (StringUtils.isNotEmpty(
				iUserRoleDao.selectUserByRoleKeyByParam(departmentManager, registerUser.getRegisterDeptid()))) {
			// 检测该部门下部门经理是否存在
			try {
				// 注册时开启注册审批工作流
				String businessId = UUID.randomUUID().toString();
				registerUser.setRegisterAuditType(0);
				if (registerUserService.saveRegisterUser(registerUser) > 0) {
					// flowId 1:成单审批 2：退单审批3:注册审批
					WorkFlowApply workFlowApply = new WorkFlowApply(0, businessId,
							3, "注册审批", null, registerUser.getRegisterId());
					if (workFlowApplyService.saveWorkFlowApply(workFlowApply) > 0) {
						// 只有在新增情况下才能开启流程
						if (StringUtils.isNull(workFlowApply.getApplyId())) {
							// 开启注册审批流程
							commonService.startProcess(businessId, workFlowApply);
							return Message.ok();
						}
					}
				}
			} catch (Exception e) {
				return Message.error();
			}
		}
		return Message.error("该部门无部门经理审核");
	}

    /**
     * 校验用户名
     */
	@Log(title = "注册", action = "校验登陆名唯一")
	@PostMapping("/checkUserNameUnique")
	@ResponseBody
	public String checkUserNameUnique(User user) {
		String uniqueFlag = "0";
		if (user != null) {
			uniqueFlag = userService.checkUserNameUnique(user.getLoginName());
		}
		return uniqueFlag;
	}
}
